package com.example.bookmanagementsystem.service;

import com.example.bookmanagementsystem.constant.Constants;
import com.example.bookmanagementsystem.entity.UserInfo;
import com.example.bookmanagementsystem.mapper.UserInfoMapper;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    public boolean userLogin(String name, String password, HttpSession session) {
        //账号或者密码为空
        if (!StringUtils.hasLength(name) || !StringUtils.hasLength(password)) {
            return false;
        }
        //账号密码正确
        UserInfo userInfo = userInfoMapper.queryUserByName(name);
        if (userInfo == null) {
            //账号或者密码错误
            return false;
        }
        if (password.equals(userInfo.getPassword())) {
            //账号或者密码正确
            userInfo.setPassword("");
            session.setAttribute(Constants.SESSION_USER_KEY, userInfo);
            return true;
        }
        //账号或者密码错误
        return false;
    }
}
